﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using RTGS.Library;
using RTGS.Library.Entities.Rtgs;
using RTGS.Library.Enum;
using RTGS.Library.Interfaces;

namespace RTGS.Library.DataAccess.Rtgs
{
    public partial class membertRepo : IRepository<membert, string>
    {
        private string errorLog;
        private string connectionString;
        public membertRepo(string ConnectionString)
        {
            errorLog = String.Empty;
            connectionString = ConnectionString;
        }
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM MEMBERT {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM MEMBERT {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM MEMBERT {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM MEMBERT {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM MEMBERT {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM MEMBERT {1}", column, parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }
        public List<membert> GetList()
        {
            List<membert> tt = new List<membert>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT memcde, accnbr, memname, bankcde, brcde1, brcde2, brcde3, brcde4, brcde5, memtype, hqflag, offlinebr, prmemcde, dtstamp, memgrp FROM MEMBERT";
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                membert t = new membert();
                                t.memcde = dr[membert.Fields.memcde] == DBNull.Value ? string.Empty : dr[membert.Fields.memcde].ToString();
                                t.accnbr = dr[membert.Fields.accnbr] == DBNull.Value ? string.Empty : dr[membert.Fields.accnbr].ToString();
                                t.memname = dr[membert.Fields.memname] == DBNull.Value ? string.Empty : dr[membert.Fields.memname].ToString();
                                t.bankcde = dr[membert.Fields.bankcde] == DBNull.Value ? string.Empty : dr[membert.Fields.bankcde].ToString();
                                t.brcde1 = dr[membert.Fields.brcde1] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde1].ToString();
                                t.brcde2 = dr[membert.Fields.brcde2] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde2].ToString();
                                t.brcde3 = dr[membert.Fields.brcde3] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde3].ToString();
                                t.brcde4 = dr[membert.Fields.brcde4] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde4].ToString();
                                t.brcde5 = dr[membert.Fields.brcde5] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde5].ToString();
                                t.memtype = dr[membert.Fields.memtype] == DBNull.Value ? string.Empty : dr[membert.Fields.memtype].ToString();
                                t.hqflag = dr[membert.Fields.hqflag] == DBNull.Value ? string.Empty : dr[membert.Fields.hqflag].ToString();
                                t.offlinebr = dr[membert.Fields.offlinebr] == DBNull.Value ? string.Empty : dr[membert.Fields.offlinebr].ToString();
                                t.prmemcde = dr[membert.Fields.prmemcde] == DBNull.Value ? string.Empty : dr[membert.Fields.prmemcde].ToString();
                                t.dtstamp = dr[membert.Fields.dtstamp] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[membert.Fields.dtstamp].ToString());
                                t.memgrp = dr[membert.Fields.memgrp] == DBNull.Value ? string.Empty : dr[membert.Fields.memgrp].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
        public List<membert> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<membert> tt = new List<membert>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize);
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" SELECT * ");
                    sbQuery.AppendFormat(" FROM (SELECT TOP({0}) ", pageSize);
                    sbQuery.AppendFormat("          r.* ");
                    sbQuery.AppendFormat("       FROM (SELECT TOP({0} + {1} - 1) memcde, accnbr, memname, bankcde, brcde1, brcde2, brcde3, brcde4, brcde5, memtype, hqflag, offlinebr, prmemcde, dtstamp, memgrp ", startRow, pageSize);
                    sbQuery.AppendFormat("              FROM MEMBERT ");
                    sbQuery.AppendFormat("             {0} ", parameters);
                    sbQuery.AppendFormat("              ORDER BY {0} {1}) r ", sortName, sortOrder);
                    sbQuery.AppendFormat("       ORDER BY {0} DESC) r ", sortName);
                    sbQuery.AppendFormat(" ORDER BY {0}", sortName);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                membert t = new membert();
                                t.memcde = dr[membert.Fields.memcde] == DBNull.Value ? string.Empty : dr[membert.Fields.memcde].ToString();
                                t.accnbr = dr[membert.Fields.accnbr] == DBNull.Value ? string.Empty : dr[membert.Fields.accnbr].ToString();
                                t.memname = dr[membert.Fields.memname] == DBNull.Value ? string.Empty : dr[membert.Fields.memname].ToString();
                                t.bankcde = dr[membert.Fields.bankcde] == DBNull.Value ? string.Empty : dr[membert.Fields.bankcde].ToString();
                                t.brcde1 = dr[membert.Fields.brcde1] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde1].ToString();
                                t.brcde2 = dr[membert.Fields.brcde2] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde2].ToString();
                                t.brcde3 = dr[membert.Fields.brcde3] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde3].ToString();
                                t.brcde4 = dr[membert.Fields.brcde4] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde4].ToString();
                                t.brcde5 = dr[membert.Fields.brcde5] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde5].ToString();
                                t.memtype = dr[membert.Fields.memtype] == DBNull.Value ? string.Empty : dr[membert.Fields.memtype].ToString();
                                t.hqflag = dr[membert.Fields.hqflag] == DBNull.Value ? string.Empty : dr[membert.Fields.hqflag].ToString();
                                t.offlinebr = dr[membert.Fields.offlinebr] == DBNull.Value ? string.Empty : dr[membert.Fields.offlinebr].ToString();
                                t.prmemcde = dr[membert.Fields.prmemcde] == DBNull.Value ? string.Empty : dr[membert.Fields.prmemcde].ToString();
                                t.dtstamp = dr[membert.Fields.dtstamp] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[membert.Fields.dtstamp].ToString());
                                t.memgrp = dr[membert.Fields.memgrp] == DBNull.Value ? string.Empty : dr[membert.Fields.memgrp].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
        public membert GetById(string key)
        {
            membert t = new membert();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT memcde, accnbr, memname, bankcde, brcde1, brcde2, brcde3, brcde4, brcde5, memtype, hqflag, offlinebr, prmemcde, dtstamp, memgrp FROM MEMBERT WHERE memcde = @memcde";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@memcde", SqlDbType.Char).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t.memcde = dr[membert.Fields.memcde] == DBNull.Value ? string.Empty : dr[membert.Fields.memcde].ToString();
                                t.accnbr = dr[membert.Fields.accnbr] == DBNull.Value ? string.Empty : dr[membert.Fields.accnbr].ToString();
                                t.memname = dr[membert.Fields.memname] == DBNull.Value ? string.Empty : dr[membert.Fields.memname].ToString();
                                t.bankcde = dr[membert.Fields.bankcde] == DBNull.Value ? string.Empty : dr[membert.Fields.bankcde].ToString();
                                t.brcde1 = dr[membert.Fields.brcde1] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde1].ToString();
                                t.brcde2 = dr[membert.Fields.brcde2] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde2].ToString();
                                t.brcde3 = dr[membert.Fields.brcde3] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde3].ToString();
                                t.brcde4 = dr[membert.Fields.brcde4] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde4].ToString();
                                t.brcde5 = dr[membert.Fields.brcde5] == DBNull.Value ? string.Empty : dr[membert.Fields.brcde5].ToString();
                                t.memtype = dr[membert.Fields.memtype] == DBNull.Value ? string.Empty : dr[membert.Fields.memtype].ToString();
                                t.hqflag = dr[membert.Fields.hqflag] == DBNull.Value ? string.Empty : dr[membert.Fields.hqflag].ToString();
                                t.offlinebr = dr[membert.Fields.offlinebr] == DBNull.Value ? string.Empty : dr[membert.Fields.offlinebr].ToString();
                                t.prmemcde = dr[membert.Fields.prmemcde] == DBNull.Value ? string.Empty : dr[membert.Fields.prmemcde].ToString();
                                t.dtstamp = dr[membert.Fields.dtstamp] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[membert.Fields.dtstamp].ToString());
                                t.memgrp = dr[membert.Fields.memgrp] == DBNull.Value ? string.Empty : dr[membert.Fields.memgrp].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }
        public bool Save(membert domain)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO MEMBERT(memcde, accnbr, memname, bankcde, brcde1, brcde2, brcde3, brcde4, brcde5, memtype, hqflag, offlinebr, prmemcde, dtstamp, memgrp) VALUES(@memcde, @accnbr, @memname, @bankcde, @brcde1, @brcde2, @brcde3, @brcde4, @brcde5, @memtype, @hqflag, @offlinebr, @prmemcde, @dtstamp, @memgrp)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@memcde", SqlDbType.Char).Value = domain.memcde;
                    cmd.Parameters.Add("@accnbr", SqlDbType.Char).Value = (object)domain.accnbr ?? DBNull.Value;
                    cmd.Parameters.Add("@memname", SqlDbType.Char).Value = (object)domain.memname ?? DBNull.Value;
                    cmd.Parameters.Add("@bankcde", SqlDbType.Char).Value = (object)domain.bankcde ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde1", SqlDbType.Char).Value = (object)domain.brcde1 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde2", SqlDbType.Char).Value = (object)domain.brcde2 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde3", SqlDbType.Char).Value = (object)domain.brcde3 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde4", SqlDbType.Char).Value = (object)domain.brcde4 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde5", SqlDbType.Char).Value = (object)domain.brcde5 ?? DBNull.Value;
                    cmd.Parameters.Add("@memtype", SqlDbType.Char).Value = (object)domain.memtype ?? DBNull.Value;
                    cmd.Parameters.Add("@hqflag", SqlDbType.Char).Value = (object)domain.hqflag ?? DBNull.Value;
                    cmd.Parameters.Add("@offlinebr", SqlDbType.Char).Value = (object)domain.offlinebr ?? DBNull.Value;
                    cmd.Parameters.Add("@prmemcde", SqlDbType.Char).Value = (object)domain.prmemcde ?? DBNull.Value;
                    cmd.Parameters.Add("@dtstamp", SqlDbType.DateTime).Value = (object)domain.dtstamp ?? DBNull.Value;
                    cmd.Parameters.Add("@memgrp", SqlDbType.Char).Value = (object)domain.memgrp ?? DBNull.Value;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }
            return result;
        }
        public bool Update(membert domain)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "UPDATE MEMBERT SET memcde = @memcde, accnbr = @accnbr, memname = @memname, bankcde = @bankcde, brcde1 = @brcde1, brcde2 = @brcde2, brcde3 = @brcde3, brcde4 = @brcde4, brcde5 = @brcde5, memtype = @memtype, hqflag = @hqflag, offlinebr = @offlinebr, prmemcde = @prmemcde, dtstamp = @dtstamp, memgrp = @memgrp WHERE memcde = @memcde";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@memcde", SqlDbType.Char).Value = domain.memcde;
                    cmd.Parameters.Add("@accnbr", SqlDbType.Char).Value = (object)domain.accnbr ?? DBNull.Value;
                    cmd.Parameters.Add("@memname", SqlDbType.Char).Value = (object)domain.memname ?? DBNull.Value;
                    cmd.Parameters.Add("@bankcde", SqlDbType.Char).Value = (object)domain.bankcde ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde1", SqlDbType.Char).Value = (object)domain.brcde1 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde2", SqlDbType.Char).Value = (object)domain.brcde2 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde3", SqlDbType.Char).Value = (object)domain.brcde3 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde4", SqlDbType.Char).Value = (object)domain.brcde4 ?? DBNull.Value;
                    cmd.Parameters.Add("@brcde5", SqlDbType.Char).Value = (object)domain.brcde5 ?? DBNull.Value;
                    cmd.Parameters.Add("@memtype", SqlDbType.Char).Value = (object)domain.memtype ?? DBNull.Value;
                    cmd.Parameters.Add("@hqflag", SqlDbType.Char).Value = (object)domain.hqflag ?? DBNull.Value;
                    cmd.Parameters.Add("@offlinebr", SqlDbType.Char).Value = (object)domain.offlinebr ?? DBNull.Value;
                    cmd.Parameters.Add("@prmemcde", SqlDbType.Char).Value = (object)domain.prmemcde ?? DBNull.Value;
                    cmd.Parameters.Add("@dtstamp", SqlDbType.DateTime).Value = (object)domain.dtstamp ?? DBNull.Value;
                    cmd.Parameters.Add("@memgrp", SqlDbType.Char).Value = (object)domain.memgrp ?? DBNull.Value;

                    try
                    {
                        conn.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return (result > 0);
        }
        public bool Delete(string key)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "DELETE FROM MEMBERT WHERE memcde = @memcde";
                    cmd.CommandType = CommandType.Text;



                    cmd.Parameters.AddWithValue("@memcde", key);// SqlDbType.Int).Value = key;




                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return result;
        }
        public string GetErrorMessage()
        {
            return errorLog;
        }

    }
}
